#!/usr/bin/env bash

cd tests/e2e

failure="false"

:> error.log

run_infection_for_dir() {
    dir=$1
    infection_executable=${2:-bin/infection}

    cd "$dir"
    echo -e "\n$dir\n";

    if [ -f composer.json ] || [ -f composer.lock ]
    then
        composer install > /dev/null 2>&1
    fi

    if [ -f "run_tests.bash" ]
    then
        output="$(INFECTION_E2E_TESTS_ENV=1 bash run_tests.bash $infection_executable 2>&1)"
    else
        output="$(INFECTION_E2E_TESTS_ENV=1 bash ../standard_script.bash $infection_executable 2>&1)"
    fi

    if [ $? != 0 ]
    then
        failure="true"
        echo "$dir" >> ../error.log
        echo "$output" >> ../error.log
        echo "" >> ../error.log
    fi
    cd ..

    if [ "$failure" = "true" ]
    then
        exit 255
    fi
}

exitOnError() {
    exit_code=$1
    msg=$2
    if [ "$exit_code" -ne 0 ]; then
        echo -e "\033[31mEXIT $exit_code $msg\033[0m" >&2
        # If any invocation of the command exits with a status of 255, xargs will stop immediately without reading any further input.
        # An error message is issued on stderr when this happens.
        exit 255
    fi
}

e2e_tests_dirs=$(find . -maxdepth 1 -mindepth 1 -type d | grep "${2:-}")

# Export the function so xargs can use it
export -f run_infection_for_dir

# Use xargs to run the run_infection_for_dir function in parallel
echo "$e2e_tests_dirs" | xargs -I {} -P 4 bash -c "run_infection_for_dir {} ${1:-bin/infection}"

exitOnError $? "One of the e2e tests failed, check the logs \n\n$(cat error.log)"

echo -e "\nSuccess"
exit 0
